package by.grsu.matrenichev.page.testing;

import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

import by.grsu.matrenichev.dao.TestingDao;
import by.grsu.matrenichev.entity.Testing;
import by.grsu.matrenichev.page.BasePage;

public class EditTestingPage extends BasePage {

	@SpringBean
	private TestingDao testingDao;

	@SuppressWarnings("serial")
	public EditTestingPage(final Testing testingToEdit) {
		super();

		// create form fields and set data
		final TextField<String> testnameField = new TextField<String>("testname", new Model<String>(testingToEdit.getTestname()));
		final TextField<String> subjectField = new TextField<String>("subject", new Model<String>(testingToEdit.getSubject()));
		final TextField<String> question1Field = new TextField<String>("question1", new Model<String>(testingToEdit.getQuestion1()));
		final TextField<String> question2Field = new TextField<String>("question2", new Model<String>(testingToEdit.getQuestion2()));
		final TextField<String> question3Field = new TextField<String>("question3", new Model<String>(testingToEdit.getQuestion3()));
		final TextField<Integer> markField = new TextField<Integer>("mark", new Model<Integer>(testingToEdit.getMark()));
		markField.setType(Integer.class);
		
		// create "form" component which is required for data retrieving from
		// client to server
		Form<Testing> form = new Form<Testing>("form", new Model<Testing>(testingToEdit)) {
			@Override
			public void onSubmit() {
				// retrieve data from the form and set it to the object
				testingToEdit.setTestname(testnameField.getModelObject());
				testingToEdit.setSubject(subjectField.getModelObject());
				testingToEdit.setQuestion1(question1Field.getModelObject());
				testingToEdit.setQuestion2(question2Field.getModelObject());
				testingToEdit.setQuestion3(question3Field.getModelObject());
				testingToEdit.setMark(markField.getModelObject());
				// save updated object in DB
				testingDao.save(testingToEdit);
				// go to the list page
				setResponsePage(ListTestingPage.class);
			}
		};

		// add form fields
		form.add(testnameField);
		form.add(subjectField);
		form.add(question1Field);
		form.add(question2Field);
		form.add(question3Field);
		form.add(markField);

		// add buttons
		form.add(new Button("saveLink"));
		form.add(new Link<Testing>("cancelLink") {
			private static final long serialVersionUID = -1521748836027504873L;

			@Override
			public void onClick() {
				setResponsePage(ListTestingPage.class);
			}
		});
		// add form to the page
		add(form);

		// add special component which will allow to display "validation errors"
		// and feedback messages
		FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
		add(feedbackPanel);
	}

}
